package com.qianli.user.application;

import com.fqgj.common.api.Response;
import com.google.common.collect.Lists;
import com.qianli.user.domain.enums.UserModelItemStatusEnum;
import com.qianli.user.domain.facatory.UserQuotaFactory;
import com.qianli.user.domain.model.UserQuotaModel;
import com.qianli.user.domain.model.quota.UserQuota;
import com.qianli.user.enums.QuotaEnum;
import com.qianli.user.ro.BaseComplete;
import com.qianli.user.ro.UserStateCode;
import com.qianli.user.ro.query.UserQuotaQueryRO;
import com.qianli.user.ro.quota.UserQuotaRO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * name:用户额度
 * auth:ting.li@qianli-inc.com
 * date:2018/1/3 20:14
 * version: v1.0.0
 **/
@Service
public class UserQuotaApplication {

    @Autowired
    private UserQuotaFactory userQuotaFactory;

    /**
     * 额度评估
     * @param queryRO
     * @return
     */
    public Response<Boolean> evaluateQuota(UserQuotaQueryRO queryRO) {

//        String userCode = queryRO.getUserCode();
//        Response<Boolean> response = null;
//
//        //判断是否有额度
//        UserQuotaRO quotaRO = load(queryRO.getUserCode(),queryRO.getCategoryCode());
//        if(null != quotaRO && quotaRO.getStatus().equals(QuotaEnum.PASS.getType())){
//            return Response.error(UserStateCode.USER_HAS_APPOINT_CATEGORY_QUOTA);
//        }
//
//        JydQuotaRequest jdyQuotaRequest = new JydQuotaRequest();
//        //基本信息
//        response = isBaseInfoSuccess(userCode,jdyQuotaRequest);
//        if(!response.isSuccess()){
//            return response;
//        }
//        //认证信息
//        response = isAuthSuccess(userCode,jdyQuotaRequest);
//        if(!response.isSuccess()){
//            return response;
//        }
//        //社交信息
//        response = isSocialSuccess(userCode,jdyQuotaRequest);
//        if(!response.isSuccess()){
//            return response;
//        }
//        //短期固定额度
//        if(queryRO.getCategoryCode().equals("sdzzall")||queryRO.getCategoryCode().equals("xjdrall")){
//            UserQuotaRO userQuota = new UserQuotaRO();
//            userQuota.setUserCode(userCode);
//            userQuota.setCategoryCode(queryRO.getCategoryCode());
//            userQuota.setStatus(QuotaEnum.PASS.getType());
//            userQuota.setGmtModified(new Date());
//            userQuota.setAppCode(queryRO.getAppCode());
//            userQuota.setBiz(queryRO.getBiz());
//            saveQuota(userQuota);
//            return Response.ok(true);
//        }
//        //调用风控评估额度
//        userRiskIntegration.evaluteQuota(jdyQuotaRequest);
        return Response.ok(true);
    }

    /**
     * 社交信息(紧急联系人+运营商)
     * @return
     */
//    private Response<Boolean> isSocialSuccess(String userCode, JydQuotaRequest jdyQuotaRequest) {
//        Response<UserSocialRO> response = userSocialApplication.load(userCode);
//        if(!response.isSuccess()){
//            LOGGER.info("query user auth info error,userCode:{}" + userCode +",param:" + JSON.toJSONString(response));
//            return Response.error(UserStateCode.getEnumByType(response.getCode()));
//        }
//        UserSocialRO userSocialRO = response.getData();
//        //紧急联系人
//        if(!isComplete(userSocialRO.getUserContactEmergency())){
//            return Response.error(UserStateCode.USER_EMERGENCY_NOT_EXISTS);
//        }
//        //通讯录
//        if(!isComplete(userSocialRO.getUserContactAddressBook())){
//            return Response.error(UserStateCode.USER_ADDRESS_BOOK_NOT_EXISTS);
//        }
//        return Response.ok(true);
//    }

//    /**
//     * 认证(实名+银行卡)
//     * @param userCode
//     * @param jdyQuotaRequest
//     * @return
//     */
//    private Response<Boolean> isAuthSuccess(String userCode, JydQuotaRequest jdyQuotaRequest) {
//        Response<UserAuthInfoRO> response = userAuthApplication.load(userCode);
//        if(!response.isSuccess()){
//            LOGGER.info("query user auth info error,userCode:{}" + userCode +",param:" + JSON.toJSONString(response));
//            return Response.error(UserStateCode.getEnumByType(response.getCode()));
//        }
//        //实名
//        UserAuthInfoRO userAuthInfoRO = response.getData();
//        if(!isComplete(userAuthInfoRO.getCustomer())){
//            return Response.error(UserStateCode.USER_AUTH_REALNAME_NOT_EXISTS);
//        }
//        //银行卡 第一张卡 默认卡
//        BankCreditRO bankCreditRO = userAuthInfoRO.getBankCredits().get(0);
//        if(!isComplete(bankCreditRO)){
//            return Response.error(UserStateCode.USER_AUTH_BANK_NOT_EXISTS);
//        }
//        //运营商
//        UserAuthCarrierRO userAuthCarrierRO = userAuthInfoRO.getUserAuthCarrier();
//        if(!isComplete(userAuthCarrierRO)){
//            return Response.error(UserStateCode.USER_CARRIER_NOT_EXISTS);
//        }
//        return Response.ok(true);
//    }

//    /**
//     * 基本信息是否完整
//     * @param userCode
//     * @param jdyQuotaRequest
//     * @return
//     */
//    private Response<Boolean> isBaseInfoSuccess(String userCode, JydQuotaRequest jdyQuotaRequest) {
//        //查询用户基本信息
//        Response<UserBaseInfoRO> ubmReponse = userBaseInfoApplication.load(userCode);
//        if(!ubmReponse.isSuccess()){
//            LOGGER.info("query user base info error,userCode:{}"+userCode+"，param:" + JSON.toJSONString(ubmReponse));
//            return Response.error(UserStateCode.getEnumByType(ubmReponse.getCode()));
//        }
//        UserBaseInfoRO userBaseInfoRO = ubmReponse.getData();
//        //基础信息
//        if(!isComplete(userBaseInfoRO.getUserBase())){
//            return Response.error(UserStateCode.USER_BASE_NOT_EXISTS);
//        }
//        //居住信息
//        if(!isComplete(userBaseInfoRO.getUserResident())){
//            return Response.error(UserStateCode.USER_RESIDENTI_NOT_EXISTS);
//        }
//        //联系方式
//        if(!isComplete(userBaseInfoRO.getUserContact())){
//            return Response.error(UserStateCode.USER_CONTACT_NOT_EXISTS);
//        }
//        //工作信息
//        if(!isComplete(userBaseInfoRO.getUserWork())){
//            return Response.error(UserStateCode.USER_WORK_NOT_EXISTS);
//        }
//        return Response.ok(true);
//    }

    private Boolean isComplete(BaseComplete complete){
        return null != complete && complete.isComplete();
    }
    /**
     * 保存额度
     * @param userQuota
     * @return
     */
    public Response<UserQuotaRO> saveQuota(UserQuotaRO userQuota) {
        //加载额度
        UserQuotaModel model = new UserQuotaModel();
        model.setUserCode(userQuota.getUserCode());
        userQuotaFactory.load(model);
        //新的额度
        UserQuota uq = new UserQuota();
        uq.setUserCode(userQuota.getUserCode());
        uq.setCategoryCode(userQuota.getCategoryCode());
        uq.setQuota(userQuota.getQuota());
        uq.setGmtModified(new Date());
        uq.setItemStatus(UserModelItemStatusEnum.ADD.getType());
        for(UserQuota uqt:model.getUserQuotas()){
            if(uqt.getCategoryCode().equals(userQuota.getCategoryCode())){
                if(uqt.getQuotaEnum().equals(QuotaEnum.PASS)){
                    uq.setItemStatus(UserModelItemStatusEnum.DEFAULT.getType());
                    return Response.error(UserStateCode.USER_HAS_APPOINT_CATEGORY_QUOTA);
                }else{
                    uq.setId(uqt.getId());
                    uq.setItemStatus(UserModelItemStatusEnum.UPDATE.getType());
                }
            }
        }
        //保存额度
        UserQuotaModel newUm = new UserQuotaModel();
        newUm.setUserCode(userQuota.getUserCode());
        newUm.setUserQuotas(Lists.newArrayList(uq));
        userQuotaFactory.store(newUm);
        return Response.ok(userQuota);
    }

    /**
     * 根据userCode和categoryCode查询额度
     * @param userCode
     * @param categoryCode
     * @return
     */
    public UserQuotaRO load(String userCode, String categoryCode) {
        //加载模型，获取所有额度
        UserQuotaModel model = new UserQuotaModel();
        model.setUserCode(userCode);
        userQuotaFactory.load(model);
        List<UserQuota> userQuotas = model.getUserQuotas();
        if(CollectionUtils.isEmpty(userQuotas)){
            return null;
        }
        //过滤不成功的
        filterQuotas(userQuotas);
        UserQuotaRO curQuota = null;
        for(UserQuota quota:userQuotas){
            if(categoryCode.equals(quota.getCategoryCode())){
                curQuota = new UserQuotaRO();
                BeanUtils.copyProperties(quota,curQuota);
                curQuota.setStatus(quota.getQuotaEnum().getType());
            }
        }
        return curQuota;
    }

    private void filterQuotas(List<UserQuota> userQuotas){
        if(CollectionUtils.isEmpty(userQuotas)){
            return;
        }
        for(int i=0;i<userQuotas.size();i++){
            if(!userQuotas.get(i).getQuotaEnum().equals(QuotaEnum.PASS)){
                userQuotas.remove(i);
                i--;
            }
        }
    }
    /**
     * 获取用户所有额度
     * @param userCode
     * @return
     */
    public List<UserQuotaRO> load(String userCode) {

        //加载模型，获取所有额度
        UserQuotaModel model = new UserQuotaModel();
        model.setUserCode(userCode);
        userQuotaFactory.load(model);
        List<UserQuota> userQuotas = model.getUserQuotas();
        if(CollectionUtils.isEmpty(userQuotas)){
            return null;
        }
        //过滤不成功的
        filterQuotas(userQuotas);
        List<UserQuotaRO> userQuotaROS = Lists.newArrayList();
        for(UserQuota quota:userQuotas){
            UserQuotaRO quotaRO = new UserQuotaRO();
            BeanUtils.copyProperties(quota,quotaRO);
            quotaRO.setStatus(quota.getQuotaEnum().getType());
            userQuotaROS.add(quotaRO);
        }
        return userQuotaROS;
    }
}
